package ck.android.englishschool;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import ck.android.englishschool.home.HomeActivity;
import ck.android.englishschool.thread.BaseThread.ThreadCallBack;
import ck.android.englishschool.thread.LoginThread;
import ck.android.englishschool.thread.RegistThread;
import ck.android.englishschool.thread.RegistThread.RegistThreadCallBack;
import ck.android.englishschool.vo.MemberVO;

public class MainActivity extends Activity implements ThreadCallBack, RegistThreadCallBack, OnClickListener{
	private Context context;
	private LoginThread loginThread;
	private RegistThread registThread;
	private AppStatus appStatus;
	private Intent intent;
	
	private RelativeLayout hideLoginLayout, hideRegistLayout;
	private LinearLayout blackLoginUpLayout, blackLoginDownLayout, blackRegistUpLayout, blackRegistDownLayout;
	private ImageButton loginBtn, registBtn, okLoginBtn, cancelLoginBtn, okRegistBtn ,cancelRegistBtn;
	private EditText userNameEdit, userPassEdit, regiNameEdit, regiPassEdit, regiConfEdit;
	
	private MemberVO memberVo;
	private boolean hideState;
	private String message;
	
	private final static int LOGIN_COMPLETED = 0;
	private final static int LOGIN_ERROR = 1;
	private final static int NETWORK_ERROR = 2;
	private final static int REGIST_COMPLETED = 3;
	private final static int REGIST_ERROR = 4;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i("main Activity", "create.....");
		setContentView(R.layout.activity_main);
		
		memberVo = new MemberVO();
		
		settingLayout();
	}
	
	public void settingLayout(){
		//context = getApplicationContext();
		context = MainActivity.this;
		appStatus = (AppStatus)context.getApplicationContext();
		
		hideLoginLayout = (RelativeLayout)findViewById(R.id.hideLoginLayout);
		hideRegistLayout = (RelativeLayout)findViewById(R.id.hideRegistLayout);
		blackLoginUpLayout = (LinearLayout)findViewById(R.id.blackLoginUpLayout);
		blackLoginDownLayout = (LinearLayout)findViewById(R.id.blackLoginDownLayout);
		blackRegistUpLayout = (LinearLayout)findViewById(R.id.blackRegistUpLayout);
		blackRegistDownLayout = (LinearLayout)findViewById(R.id.blackRegistDownLayout);
		
		loginBtn = (ImageButton)findViewById(R.id.loginBtn);
		registBtn = (ImageButton)findViewById(R.id.registBtn);
		okLoginBtn = (ImageButton)findViewById(R.id.okLoginBtn);
		cancelLoginBtn = (ImageButton)findViewById(R.id.cancelLoginBtn);
		okRegistBtn = (ImageButton)findViewById(R.id.okRegistBtn);
		cancelRegistBtn = (ImageButton)findViewById(R.id.cancelRegistBtn);
		
		userNameEdit = (EditText)findViewById(R.id.userNameEdit);
		userPassEdit = (EditText)findViewById(R.id.userPassEdit);
		regiNameEdit = (EditText)findViewById(R.id.regiNameEdit);
		regiPassEdit = (EditText)findViewById(R.id.regiPassEdit);
		regiConfEdit = (EditText)findViewById(R.id.regiConfEdit);
		
		loginBtn.setOnClickListener(this);
		registBtn.setOnClickListener(this);
		okLoginBtn.setOnClickListener(this);
		cancelLoginBtn.setOnClickListener(this);
		okRegistBtn.setOnClickListener(this);
		cancelRegistBtn.setOnClickListener(this);
		
		init();
		
	}
	
	public void init(){
		//블랙레이아웃 색을 칠한다.
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setAlpha(200);
		blackLoginUpLayout.setBackgroundColor(paint.getColor());
		blackLoginDownLayout.setBackgroundColor(paint.getColor());
		blackRegistUpLayout.setBackgroundColor(paint.getColor());
		blackRegistDownLayout.setBackgroundColor(paint.getColor());
	}
	
	private void loginLayoutHidden(boolean state){
		if(state){
			hideLoginLayout.setVisibility(View.GONE);
			blackLoginUpLayout.setVisibility(View.GONE);
			blackLoginDownLayout.setVisibility(View.GONE);
		}else{
			hideLoginLayout.setVisibility(View.VISIBLE);
			blackLoginUpLayout.setVisibility(View.VISIBLE);
			blackLoginDownLayout.setVisibility(View.VISIBLE);
		}
	}
	
	private void regiLayoutHidden(boolean state){
		if(state){
			hideRegistLayout.setVisibility(View.GONE);
			blackRegistUpLayout.setVisibility(View.GONE);
			blackRegistDownLayout.setVisibility(View.GONE);
		}else{
			hideRegistLayout.setVisibility(View.VISIBLE);
			blackRegistUpLayout.setVisibility(View.VISIBLE);
			blackRegistDownLayout.setVisibility(View.VISIBLE);
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.loginBtn:
			if(hideLoginLayout.getVisibility() == View.GONE){
				hideState = false;
			}
			loginLayoutHidden(hideState);
			break;
			
		case R.id.registBtn:
			if(hideRegistLayout.getVisibility() == View.GONE){
				hideState = false;
			}
			regiLayoutHidden(hideState);
			break;
			
		case R.id.okLoginBtn:
			startLogin();
			
			/*if(!appStatus.checkInval(context, userNameEdit, "아이디")){
				return;
			}else if(!appStatus.checkInval(context, userPassEdit, "패스워드")){
				return;
			}else{
				startLogin();
			}*/
			
			break;
			
		case R.id.cancelLoginBtn:
			if(hideLoginLayout.getVisibility() == View.VISIBLE){
				hideState = true;
			}
			loginLayoutHidden(hideState);
			break;
			
		case R.id.okRegistBtn:
			if(!appStatus.checkInval(context, regiNameEdit, "아이디")){
				return;
			}else if(!appStatus.checkInval(context, regiPassEdit, "패스워드")){
				return;
			}else if(!regiPassEdit.getText().toString().trim().equals(regiConfEdit.getText().toString().trim())){
				appStatus.utilm.ShowDialog(context, "실패", "비밀번호가 맞지 않습니다", 20);
				return;
			}else{
				startRegist();
			}
			break;
			
		case R.id.cancelRegistBtn:
			if(hideRegistLayout.getVisibility() == View.VISIBLE){
				hideState = true;
			}
			regiLayoutHidden(hideState);
			break;
			
		default:
			break;
		}
	}
	
	public void startLogin(){
		/*memberVo.setUserId(userNameEdit.getText().toString());
		memberVo.setUserPass(userPassEdit.getText().toString());
		
		String m = getApplicationContext().getString(R.string.loginchk);
		appStatus.showDialogNo(m, this);*/
		
		//test
		intent = new Intent(MainActivity.this, HomeActivity.class);
		//intent.putExtra("recNo", quarantineDetailItem.getRecNo());
		startActivity(intent);
		
		//main
		/*loginThread = new LoginThread(context, this, memberVo);
		loginThread.start();*/
		
		finish();
		
	}
	
	public void startRegist(){
		memberVo.setUserId(regiNameEdit.getText().toString());
		memberVo.setUserPass(regiPassEdit.getText().toString());
		
		String m = getApplicationContext().getString(R.string.registchk);
		appStatus.showDialogNo(m, this);
		
		registThread = new RegistThread(context, this, memberVo);
		registThread.start();
	}
	
	public void movePage(){
		Log.i("회원정보", appStatus.getMemberVo().toString());
		startActivity(new Intent(MainActivity.this, HomeActivity.class));
    	finish();
	}
	
	@Override
	public void threadError(String message) {
		if(appStatus.dialog !=null && appStatus.dialog.isShowing()) {
			appStatus.dialog.dismiss();
		}
		Message msg = handler.obtainMessage();
		msg.what = LOGIN_ERROR;
		if(null != message && !"".equals(message)) {
			msg.obj = message;
		}
		Log.i("errorItem.getrDesc()", message);
		handler.sendMessage(msg);
	}
	
	@Override
	public void threadCompleted(boolean isNetworkConntected,
			Object memberVo, String message) {
		if(appStatus.dialog !=null && appStatus.dialog.isShowing()) {
			appStatus.dialog.dismiss();
		}
		
		//네트워크 접속이 되어 있지 않을 때
		if(!isNetworkConntected){	
			Message msg = handler.obtainMessage();
			msg.what = NETWORK_ERROR;
			handler.sendMessage(msg);
			return;
		}
		//접속이 되어있을때
		else{
			this.memberVo = (MemberVO) memberVo;
			this.message = message;
			Message msg = handler.obtainMessage();
			msg.what = LOGIN_COMPLETED;
			handler.sendMessage(msg);
		}
	}

	@Override
	public void OnRegistThreadError(String message) {
		if(appStatus.dialog !=null && appStatus.dialog.isShowing()) {
			appStatus.dialog.dismiss();
		}
		Message msg = handler.obtainMessage();
		msg.what = REGIST_ERROR;
		if(null != message && !"".equals(message)) {
			msg.obj = message;
		}
		Log.i("errorItem.getrDesc()", message);
		handler.sendMessage(msg);
	}
	
	@Override
	public void OnRegistThreadCompleted(boolean isNetworkConntected,
			MemberVO memberVo, String message) {
		if(appStatus.dialog !=null && appStatus.dialog.isShowing()) {
			appStatus.dialog.dismiss();
		}
		
		//네트워크 접속이 되어 있지 않을 때
		if(!isNetworkConntected){	
			Message msg = handler.obtainMessage();
			msg.what = NETWORK_ERROR;
			handler.sendMessage(msg);
			return;
		}
		//접속이 되어있을때
		else{
			this.memberVo = memberVo;
			this.message = message;
			Message msg = handler.obtainMessage();
			msg.what = REGIST_COMPLETED;
			handler.sendMessage(msg);
		}
	}
	
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(final Message msg) {
			switch(msg.what){
			
			case LOGIN_COMPLETED:
				appStatus.utilm.ShowDialog(context, "성공", message, 20);
				movePage();
				break;

			case LOGIN_ERROR:
				if(null == message ||"".equals(message)){
					message = "서버와의 연결에 실패하였습니다.";
				}else{
					message = msg.obj.toString();	
				}
				appStatus.utilm.ShowDialog(context, "실패", message, 20);
				break;

			case NETWORK_ERROR:
				appStatus.utilm.ShowDialog(context, "실패", "네트워크에러", 20);
				break;
				
			case REGIST_COMPLETED:
				appStatus.utilm.ShowDialog(context, "성공", message, 20);
				movePage();
				break;
				
			case REGIST_ERROR:
				if(null == message ||"".equals(message)){
					message = "서버와의 연결에 실패하였습니다.";
				}else{
					message = msg.obj.toString();	
				}
				appStatus.utilm.ShowDialog(context, "실패", message, 20);
				break;
				
			default:
				break;
			}
		}
	};

}